package cn.iocoder.yudao.module.yyzx.controller.admin.cfood;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.cfood.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.cfood.CFoodDO;
import cn.iocoder.yudao.module.yyzx.service.cfood.CFoodService;

@Tag(name = "管理后台 - 客户膳食信息")
@RestController
@RequestMapping("/yyzx/C-food")
@Validated
public class CFoodController {

    @Resource
    private CFoodService cFoodService;

    @PostMapping("/create")
    @Operation(summary = "创建客户膳食信息")
    @PreAuthorize("@ss.hasPermission('yyzx:C-food:create')")
    public CommonResult<Integer> createCFood(@Valid @RequestBody CFoodSaveReqVO createReqVO) {
        return success(cFoodService.createCFood(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户膳食信息")
    @PreAuthorize("@ss.hasPermission('yyzx:C-food:update')")
    public CommonResult<Boolean> updateCFood(@Valid @RequestBody CFoodSaveReqVO updateReqVO) {
        cFoodService.updateCFood(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户膳食信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:C-food:delete')")
    public CommonResult<Boolean> deleteCFood(@RequestParam("id") Integer id) {
        cFoodService.deleteCFood(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户膳食信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:C-food:query')")
    public CommonResult<CFoodRespVO> getCFood(@RequestParam("id") Integer id) {
        CFoodDO cFood = cFoodService.getCFood(id);
        return success(BeanUtils.toBean(cFood, CFoodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户膳食信息分页")
    @PreAuthorize("@ss.hasPermission('yyzx:C-food:query')")
    public CommonResult<PageResult<CFoodRespVO>> getCFoodPage(@Valid CFoodPageReqVO pageReqVO) {
        PageResult<CFoodDO> pageResult = cFoodService.getCFoodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CFoodRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户膳食信息 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:C-food:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCFoodExcel(@Valid CFoodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CFoodDO> list = cFoodService.getCFoodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户膳食信息.xls", "数据", CFoodRespVO.class,
                        BeanUtils.toBean(list, CFoodRespVO.class));
    }

}